package com.ktgj.customer.service.impl;

import java.util.Date;
import java.util.List;

import com.ktgj.common.core.constant.BusinessTypeConstants;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.customer.domain.*;
import com.ktgj.customer.domain.vo.CustomerIdVo;
import com.ktgj.customer.mapper.FinanceOrderRefundmentMapper;
import com.ktgj.customer.mapper.MallOrderMapper;
import com.ktgj.customer.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.MallOrderRefundMapper;

/**
 * 退单Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-15
 */
@Service
public class MallOrderRefundServiceImpl implements IMallOrderRefundService {
    @Autowired
    private MallOrderRefundMapper mallOrderRefundMapper;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IMallOrderProductService mallOrderProductService;
    @Autowired
    private IMallProductService mallProductService;
    @Autowired
    private IMallProductSpecService mallProductSpecService;
    @Autowired
    private MallOrderMapper mallOrderMapper;
    @Autowired
    private FinanceOrderRefundmentMapper financeOrderRefundmentMapper;

    /**
     * 查询退单
     *
     * @param refundId 退单ID
     * @return 退单
     */
    @Override
    public MallOrderRefund selectMallOrderRefundById(Long refundId) {
        return mallOrderRefundMapper.selectMallOrderRefundById(refundId);
    }

    /**
     * 查询退单列表
     *
     * @param mallOrderRefund 退单
     * @return 退单
     */
    @Override
    public List<MallOrderRefund> selectMallOrderRefundList(MallOrderRefund mallOrderRefund) {
        return mallOrderRefundMapper.selectMallOrderRefundList(mallOrderRefund);
    }

    /**
     * 新增退单
     *
     * @param mallOrderRefund 退单
     * @return 结果
     */
    @Override
    public int insertMallOrderRefund(MallOrderRefund mallOrderRefund) {
        mallOrderRefund.setCreateTime(DateUtils.getNowDate());
        return mallOrderRefundMapper.insertMallOrderRefund(mallOrderRefund);
    }

    /**
     * 修改退单
     *
     * @param mallOrderRefund 退单
     * @return 结果
     */
    @Override
    public int updateMallOrderRefund(MallOrderRefund mallOrderRefund) {
        int orderRefund = mallOrderRefundMapper.updateMallOrderRefund(mallOrderRefund);
        if (orderRefund > 0) {
            MallOrderRefund refund = this.selectMallOrderRefundById(mallOrderRefund.getRefundId());
            FinanceOrderRefundment refundment = new FinanceOrderRefundment();
            refundment.setOrderTypeId(Long.valueOf(com.ktgj.common.core.constant.FinanceOrderRefundment.mall.getValue()));
            refundment.setCustomerId(refund.getCustomerId());
            refundment.setRefundmentType(Long.valueOf(com.ktgj.common.core.constant.FinanceOrderRefundment.originalRoad.getValue()));
            refundment.setRefundmentAmount(refund.getRefundAmount());
            refundment.setStatus(com.ktgj.common.core.constant.FinanceOrderRefundment.initial.toString());
            refundment.setOrderId(String.valueOf(refund.getOrderId()));
            refundment.setCreateTime(new Date());
            MallOrder mallOrder = mallOrderMapper.selectMallOrderById(refund.getOrderId());
            if (mallOrder != null)
            {
                if (mallOrder.getPayType() != null)
                {
                    if (mallOrder.getPayType().equals(BusinessTypeConstants.BankCredit.getValue()))
                    {
                        refundment.setPaymentTypeId("2");
                    } else if (mallOrder.getPayType().equals(BusinessTypeConstants.BankDebit.getValue()))
                    {
                        refundment.setPaymentTypeId("1");
                    }
                    if (mallOrder.getPayType().equals(BusinessTypeConstants.Wechat.getValue()))
                    {
                        refundment.setPaymentTypeId("3");
                    }
                }
                refundment.setSerialnumber(mallOrder.getTransactionId());
            }
            financeOrderRefundmentMapper.insertFinanceOrderRefundment(refundment);
        }
        return orderRefund;
    }

    /**
     * 批量删除退单
     *
     * @param refundIds 需要删除的退单ID
     * @return 结果
     */
    @Override
    public int deleteMallOrderRefundByIds(Long[] refundIds) {
        return mallOrderRefundMapper.deleteMallOrderRefundByIds(refundIds);
    }

    /**
     * 删除退单信息
     *
     * @param refundId 退单ID
     * @return 结果
     */
    @Override
    public int deleteMallOrderRefundById(Long refundId) {
        return mallOrderRefundMapper.deleteMallOrderRefundById(refundId);
    }

    /**
     * 商城退单列表
     *
     * @param search
     * @return
     */
    @Override
    public List<MallOrderBackstage> queryMallOrderRefunds(MallOrderSearch search) {
        List<MallOrderBackstage> mallOrderRefunds = mallOrderRefundMapper.queryMallOrderRefunds(search);
        if (mallOrderRefunds != null && mallOrderRefunds.size() > 0) {
            mallOrderRefunds.forEach(orderRefund -> {
                Customer customer = customerService.selectCustomerById(orderRefund.getCustomerId());
                if (customer.getNickName() != null && !customer.getNickName().equals("")) {
                    orderRefund.setNickName(customer.getNickName());
                } else {
                    orderRefund.setNickName("未知");
                }
                if (customer.getCustomerName() != null && !customer.getCustomerName().equals("")) {
                    orderRefund.setCustomerName(customer.getCustomerName());
                } else {
                    orderRefund.setCustomerName("未知");
                }
                orderRefund.setContractPhoneOne(customer.getContractPhoneOne());
                CustomerIdVo customerIdVo = new CustomerIdVo();
                try {
                    customerIdVo.setCustomerId(orderRefund.getCustomerId());
                    CustomerIdVo idVo = customerService.customerId(customerIdVo);
                    orderRefund.setCardCode(idVo.getCustomerCard());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                MallOrderProduct product = new MallOrderProduct();
                product.setOrderId(orderRefund.getOrderId());
                List<MallOrderProduct> mallOrderProducts = mallOrderProductService.selectMallOrderProductList(product);
                orderRefund.setProductNumber(mallOrderProducts.size());
                if (mallOrderProducts != null && mallOrderProducts.size() > 0) {
                    for (MallOrderProduct mallOrderProduct : mallOrderProducts) {
                        MallProduct mallProduct = mallProductService.selectMallProductById(mallOrderProduct.getProductId());
                        MallProductSpec mallProductSpec = mallProductSpecService.selectMallProductSpecById(mallOrderProduct.getProductSpecId());
                        String productName = mallProduct.getProductName() + "·" + mallProductSpec.getSpecName();
                        orderRefund.getProductName().add(productName);
                        if (search.getSupplierId() != null && search.getSupplierId().intValue() == mallProduct.getSupplierId().intValue()) {
                            MallSupplierProduct mallSupplier = new MallSupplierProduct();
                            mallSupplier.setOrderProductId(mallOrderProduct.getOrderProductId());
                            mallSupplier.setProductName(mallProduct.getProductName());
                            mallSupplier.setSupplierId(mallProduct.getSupplierId());
                            mallSupplier.setExpressNo(mallOrderProduct.getExpressNo());
                            orderRefund.getLstSupplierProduct().add(mallSupplier);
                        }
                    }
                }
            });
        }
        return mallOrderRefunds;
    }
}
